/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace Oceanside
{
    /// <summary>
    /// Helper to show or close given splash window
    /// </summary>
    public static class Splasher
    {
        /// <summary>
        /// 
        /// </summary>
        private static Window mSplash;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        private delegate void SplashStatusDelegateType(String message, bool stallCounter);

        /// <summary>
        /// Get or set the splash screen window
        /// </summary>
        public static Window Splash
        {
            get
            {
                return mSplash;
            }
            set
            {
                mSplash = value;
            }
        }

        private static decimal totalWorkSteps = 0;

        /// <summary>
        /// Show splash screen
        /// </summary>
        public static void ShowSplash(decimal totalInitSteps)
        {
            totalWorkSteps = totalInitSteps;
            if (mSplash != null)
            {
                mSplash.Show();
            }
        }
        /// <summary>
        /// Close splash screen
        /// </summary>
        public static void CloseSplash()
        {
            if (mSplash != null)
            {
                mSplash.Close();

                if (mSplash is IDisposable)
                    (mSplash as IDisposable).Dispose();
            }
        }

        private static int initStageCounter = 0;
        public static int CurrentStepValue
        {
            get { return Splasher.initStageCounter; }
            set { Splasher.initStageCounter = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private static string _lastStatusMessage = "";

        /// <summary>
        /// 
        /// </summary>
        public static void UpdateSplashStatus()
        {
            UpdateSplashStatus_NOTHROW(_lastStatusMessage);
        }

        /// <summary>
        /// 
        /// </summary>
        public static void UpdateSplashStatus_NOTHROW(String message, bool stallCounter = false)
        {
            try
            {
                _lastStatusMessage = message;

                if (Splasher.Splash.Dispatcher.CheckAccess())
                {
                    if (!stallCounter)
                    {
                        initStageCounter++;
                    }

                    previousPercent = Convert.ToInt32(((decimal)initStageCounter / totalWorkSteps) * 100);
                    MessageListener.Instance.ReceiveMessage(message, previousPercent);                    
                }
                else
                {
                    Splasher.Splash.Dispatcher.Invoke(new SplashStatusDelegateType(UpdateSplashStatus_NOTHROW), message, stallCounter);
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SPLASHER_UPDATE_STATUS, exc);
            }
        }

        private static int previousPercent = 0;
        /// <summary>
        /// 
        /// </summary>
        public static void SetSplashStatusNoUpdate(String message)
        {
            try
            {
                MessageListener.Instance.ReceiveMessage(message, previousPercent);
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SPLASHER_SET_SPLASHER_STATUS, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        private static void da_Completed_NOTHROW(object sender, EventArgs e)
        {
            try
            {
                CloseSplash();
                MessageListener.Instance.Dispose();
                MessageListener.Instance = null;
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SPLASHER_TIMER_COMPLETED, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void StartFadeOut()
        {
            ///Fade out animation.
            Storyboard sb = new Storyboard();
            DoubleAnimation da = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(5)));
            da.Completed += new EventHandler(da_Completed_NOTHROW);
            Storyboard.SetTargetProperty(da, new PropertyPath(Window.OpacityProperty));
            Storyboard.SetTargetName(da, Splash.Name);
            sb.Children.Add(da);
            sb.Begin(Splash);
        }
    }
}
